/* =========================================================================
 *
 * Game.js
 *  This script contains the main control loop for the Game, which passes 
 *  Entities to the App that is currently active.
 *
 * ========================================================================= */
ECS.Game = function Game (){
    
    var self       = this, 
    	activeApp;

    // Game is running
    this._running = true;
    
    // Create the Entity Watcher first
    
    
    // Setup the references to the entities
    ECS.entities = {};
    
    

    // Set the active App to the Main Menu
    ECS.Game.activeApp = new ECS.MainMenu();
    
    
    // Game loop
    function gameLoop (){
        // Run the systems.
    	if (ECS.Game.activeApp && ECS.Game.activeApp.runSystems) {
    		ECS.Game.activeApp.runSystems();
    	}
    	else {
    		console.log ("** ERROR - No active App or missing runSystems method");
    		this._running = false;
    	}
    	
        // continue the loop
        if(self._running !== false){
            requestAnimationFrame(gameLoop);
        }
    }
    
    // Start the game loop
    requestAnimationFrame(gameLoop);

    return this;
};

// Start the game
ECS.game = new ECS.Game();